#include "main/utils.h"
using namespace std;

int peak(const vector<int>& vec) {
  int begin = 0;
  int end = vec.size() - 1;
  while (begin <= end) {
    size_t mid = (end + begin) / 2;
    if (mid == 0 && vec[mid] > vec[mid + 1]) {
      return 0;
    }
    if (mid == vec.size() - 1 && vec[mid] > vec[mid - 1]) {
      return vec.size() - 1;
    }
    if (vec[mid] >= vec[mid - 1] && vec[mid] >= vec[mid + 1]) {
      return mid;
    }
    if (vec[mid] < vec[mid - 1]) {
      end = mid - 1;
    }
    if (vec[mid] > vec[mid - 1]) {
      begin = mid + 1;
    }
  }
  return -1;
}

int main() {
  vector<int> vec = {1, 3, 5, 4, 2};
  cout << peak(vec) << endl;

  vector<int> vec1 = {1, 2, 3, 4, 5};
  cout << peak(vec1) << endl;

  vector<int> vec2 = {5, 4, 3, 2, 1};
  cout << peak(vec2) << endl;

  return 0;
}
